﻿namespace PowerSong.SongDatabase.ObjectModel
    {
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// A class that enables songs to be created, edited, retrieved and deleted.
    /// </summary>
    public class Songs : ObjectModelBase
        {

        internal Songs(Database database) : base(database) { }

        /// <summary>
        /// Gets all of the songs that are available in the database.
        /// </summary>
        /// <returns>A list of all of the songs available in the database.</returns>
        public Song[] GetAllSongs()
            {

            var result = new List<Song>();
            foreach (var nextSong in Database.Filter<Song>())
                {
                result.Add(nextSong);
                }
            return result.ToArray();

            }

        /// <summary>
        /// Returns the number of songs that have been definied in the database.
        /// </summary>
        /// <returns>The number of available songs in the database.</returns>
        public int Count()
            {
            return Database.Filter<Song>().Count;
            }

        /// <summary>
        /// Gets the song whose ID is the same as the one given.
        /// </summary>
        /// <param name="identifier">The ID of the song to retrieve.</param>
        /// <returns>The requested song, or NULL if there is no matching song in the database with the given ID.</returns>
        public Song Get(Guid identifier)
            {
            return Database.Get<Song>(identifier);
            }

        /// <summary>
        /// Determines whether or not the given song exists in the database. The song's ID is used to determine existence.
        /// </summary>
        /// <param name="song">The song to search for in the database.</param>
        /// <returns>True, if the song exists in the database, or if a NULL song has been provided. False otherwise.</returns>
        public bool Contains(Song song)
            {
            if (song == null) return false;
            return Get(song.ID) != null;
            }

        /// <summary>
        /// Determines whether or not the given song exists in the database.
        /// </summary>
        /// <param name="songID">The ID of the song to search for in the database.</param>
        /// <returns>True, if the song exists in the database. False otherwise.</returns>
        public bool Contains(Guid songID)
            {
            return Get(songID) != null;
            }

        /// <summary>
        /// Creates a new song, or effectively edits an existing song.
        /// To create a song, supply an instance of the Song class that has been constructed.
        /// To edit an existing song, retrieve it from the database first, modify its fields, then supply that instance instead.
        /// </summary>
        /// <param name="song">The song to write to the database.</param>
        public void Write(Song song)
            {
            Database.Write(song);
            }

        /// <summary>
        /// Deletes an existing song from the database.
        /// </summary>
        /// <param name="song">The song to delete from the database.</param>
        public void Delete(Song song)
            {
            Database.Delete(song);
            }

        }

    }
